<template>
  <basic-container>
    <div class="avue-crud">
      <el-form :inline="true">
        <el-form-item>
          <el-button  icon="el-icon-plus" type="primary"
                     @click="addOrUpdateHandle(false)">
            添加
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="menuList"
        :tree-props="{children: 'children', hasChildren: 'hasChildrens'}"
        border
        row-key="id">
        <el-table-column :show-overflow-tooltip="true" label="菜单名称" prop="name" width="180"></el-table-column>
        <el-table-column align="center" label="图标" prop="icon" width="100">
          <template slot-scope="scope">
            <i :class="scope.row.icon"/>
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" width="60"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="组件路径" prop="path"></el-table-column>
        <el-table-column align="center" label="类型" prop="type" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === '0'" type="success">菜单</el-tag>
            <el-tag v-if="scope.row.type === '1'" type="info">按钮</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="缓冲" prop="keepAlive" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.keepAlive === '0'" type="info">关闭</el-tag>
            <el-tag v-if="scope.row.keepAlive === '1'" type="success">开启</el-tag>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="权限标识" prop="permission"></el-table-column>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="permissions.sys_menu_add"
              icon="el-icon-plus"
              type="text"
              @click="addOrUpdateHandle(false,scope.row.id)">添加
            </el-button>
            <el-button v-if="permissions.sys_menu_edit"
                       icon="el-icon-edit"
                       type="text"
                       @click="addOrUpdateHandle(true,scope.row.id)">修改
            </el-button>
            <el-button v-if="permissions.sys_menu_del" icon="el-icon-delete" type="text"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></table-form>
    </div>
  </basic-container>
</template>

<script>
import {delObj, fetchMenuTree} from '@/api/admin/menu'
import TableForm from './menu-form'
import {mapGetters} from 'vuex'

export default {
  name: "Menu",
  components: {TableForm},
  data() {
    return {
      addOrUpdateVisible: false,
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  methods: {
    addOrUpdateHandle(isEdit, id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(isEdit, id)
      })
    },
    getList() {
      this.loading = true;
      fetchMenuTree(false).then(response => {
        this.menuList = response.data.data
        this.loading = false;
      });
    },
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delObj(row.id);
      }).then(() => {
        this.getList();
        this.$message.success('删除成功')
      })
    }
  }
};
</script>
